package com.ruoyi.basebill.controller;

/**
 * @projectName: wfz
 * @package: com.ruoyi.basebill.controller
 * @className: PushGzcl
 * @author: Eric
 * @description: TODO
 * @date: 2023/1/10 14:05
 * @version: 1.0
 */

import com.ruoyi.basebill.domain.TWfzRemuneration;
import com.ruoyi.basebill.service.ITWfzRemunerationService;
import com.ruoyi.basebill.service.billIssue.IPushGzclService;
import com.ruoyi.basebill.service.exception.DataNullException;
import com.ruoyi.common.core.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/basebill/Gzcl")
public class PushGzclController {

    @Autowired
    private ITWfzRemunerationService tWfzRemunerationService;
    @Autowired
    private IPushGzclService pushGzclService;

    @GetMapping("/push")
    public AjaxResult importRemuneration(@PathVariable long fid){

        //工资发放下发
        TWfzRemuneration tWfzRemuneration = tWfzRemunerationService.selectTWfzRemunerationByFid(fid);
        try {
        //审核情况下才下发
        String fstate = tWfzRemuneration.getFstate();
        if(!"AUDIT".equals(fstate)){
            return AjaxResult.error("该单据未审核，禁止下发");
        }
            StringBuffer buffer = new StringBuffer();
            pushGzclService.pushGzff(fid,buffer);
            pushGzclService.pushGzft(fid,buffer);
            pushGzclService.pushGzjt(fid,buffer);
            //没有抛出异常则表明单据是正常下发
            tWfzRemuneration.setFreason("下发成功");
            tWfzRemuneration.setFiscreatebill(1l);
        } catch (DataNullException e) {
            //抛出异常则返回前端异常信息，并且记录原因
            tWfzRemuneration.setFreason(e.getMessage());
            tWfzRemuneration.setFiscreatebill(0l);
            return AjaxResult.error("下发失败,具体失败信息，请查看单据失败原因");
        }
        return AjaxResult.success("该单据下发成功");
    }
}
